﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using LoA.Shared.Interfaces;
using LoA.Shared.Physics;

namespace LoA.PhysicsEngine {
	public partial class PhysicsComponent : DrawableGameComponent, IPhysics {
		private static PhysicsComponent _current = null;
		public static PhysicsComponent Current { get { return _current; } }

		ICamera Camera;

		public PhysicsComponent(Game game, ICamera camera)
			: base(game) {
			if (_current == null)
				_current = this;
			this.Camera = camera;
			this.objects = new List<IPhysicalBody>( );
		}

		List<IPhysicalBody> objects;

		IPhysicalBody pointModel;

		public override void Initialize( ) {
			base.Initialize( );
		}

		protected override void LoadContent( ) {
			var p1 = new PhysicsObject(this.Game, new Vector3(20, 30, 5));
			var p2 = new PhysicsObject(this.Game, new Vector3(0, 30, 6));

			pointModel = new PhysicsObject(this.Game, new Vector3(20, 20, 20));

			//objects.Add(p1);
			//objects.Add(p2);

			//this.Hit(p1, new Vector3(-1, 0, 0), -5.5f);
			//this.Hit(p2, new Vector3(1, 0, 0), -2.1f);

			objects.Add(new PhysicalBody(this.Game.Content.Load<Microsoft.Xna.Framework.Graphics.Model>(@"World\Tropical\Model\Agava\agava-001"), new Vector3(0, 0, 10), Matrix.Identity));
			objects.Add(new PhysicalBody(this.Game.Content.Load<Microsoft.Xna.Framework.Graphics.Model>(@"World\Tropical\Model\Agava\agava-001"), new Vector3(0, 0, 20), Matrix.Identity));
			objects.Add(new PhysicalBody(this.Game.Content.Load<Microsoft.Xna.Framework.Graphics.Model>(@"World\Tropical\Model\Aloe\aloe-001"), new Vector3(0, 0, 30), Matrix.Identity));
			objects.Add(new PhysicalBody(this.Game.Content.Load<Microsoft.Xna.Framework.Graphics.Model>(@"World\Tropical\Model\Aloe\aloe-001"), new Vector3(0, 0, 40), Matrix.Identity));
			objects.Add(new PhysicalBody(this.Game.Content.Load<Microsoft.Xna.Framework.Graphics.Model>(@"World\Tropical\Model\Bamboo\bamboo-001"), new Vector3(0, 0, 50), Matrix.Identity));
			objects.Add(new PhysicalBody(this.Game.Content.Load<Microsoft.Xna.Framework.Graphics.Model>(@"World\Tropical\Model\Bamboo\bamboo-002"), new Vector3(0, 0, 60), Matrix.Identity));
			objects.Add(new PhysicalBody(this.Game.Content.Load<Microsoft.Xna.Framework.Graphics.Model>(@"World\Tropical\Model\Banana\banana-001"), new Vector3(0, 0, 70), Matrix.Identity));
			objects.Add(new PhysicalBody(this.Game.Content.Load<Microsoft.Xna.Framework.Graphics.Model>(@"World\Tropical\Model\Banana\banana-002"), new Vector3(0, 0, 80), Matrix.Identity));
			objects.Add(new PhysicalBody(this.Game.Content.Load<Microsoft.Xna.Framework.Graphics.Model>(@"World\Tropical\Model\BirdOfPar\birdOfPar-001"), new Vector3(0, 0, 90), Matrix.Identity));
			objects.Add(new PhysicalBody(this.Game.Content.Load<Microsoft.Xna.Framework.Graphics.Model>(@"World\Tropical\Model\BirdOfPar\birdOfPar-001"), new Vector3(0, 0, 100), Matrix.Identity));
			objects.Add(new PhysicalBody(this.Game.Content.Load<Microsoft.Xna.Framework.Graphics.Model>(@"World\Tropical\Model\Bloss\bloss-001"), new Vector3(0, 0, 110), Matrix.Identity));
			objects.Add(new PhysicalBody(this.Game.Content.Load<Microsoft.Xna.Framework.Graphics.Model>(@"World\Tropical\Model\Bloss\bloss-002"), new Vector3(0, 0, 120), Matrix.Identity));
			objects.Add(new PhysicalBody(this.Game.Content.Load<Microsoft.Xna.Framework.Graphics.Model>(@"World\Tropical\Model\CrotonBloss\crotonBloss-001"), new Vector3(0, 0, 130), Matrix.Identity));
			objects.Add(new PhysicalBody(this.Game.Content.Load<Microsoft.Xna.Framework.Graphics.Model>(@"World\Tropical\Model\CrotonBloss\crotonBloss-001"), new Vector3(0, 0, 140), Matrix.Identity));
			objects.Add(new PhysicalBody(this.Game.Content.Load<Microsoft.Xna.Framework.Graphics.Model>(@"World\Tropical\Model\Flax\flax-001"), new Vector3(0, 0, 150), Matrix.Identity));
			objects.Add(new PhysicalBody(this.Game.Content.Load<Microsoft.Xna.Framework.Graphics.Model>(@"World\Tropical\Model\Flax\flax-002"), new Vector3(0, 0, 160), Matrix.Identity));
			objects.Add(new PhysicalBody(this.Game.Content.Load<Microsoft.Xna.Framework.Graphics.Model>(@"World\Tropical\Model\Grass\grass-001"), new Vector3(0, 0, 170), Matrix.Identity));
			objects.Add(new PhysicalBody(this.Game.Content.Load<Microsoft.Xna.Framework.Graphics.Model>(@"World\Tropical\Model\Grass\grass-001"), new Vector3(0, 0, 180), Matrix.Identity));
			objects.Add(new PhysicalBody(this.Game.Content.Load<Microsoft.Xna.Framework.Graphics.Model>(@"World\Tropical\Model\Tree\tree-001"), new Vector3(0, 0, 190), Matrix.Identity));
			objects.Add(new PhysicalBody(this.Game.Content.Load<Microsoft.Xna.Framework.Graphics.Model>(@"World\Tropical\Model\Tree\tree-002"), new Vector3(0, 0, 200), Matrix.Identity));
			objects.Add(new PhysicalBody(this.Game.Content.Load<Microsoft.Xna.Framework.Graphics.Model>(@"World\Tropical\Model\Yuca\yuca-001"), new Vector3(0, 0, 210), Matrix.Identity));
			objects.Add(new PhysicalBody(this.Game.Content.Load<Microsoft.Xna.Framework.Graphics.Model>(@"World\Tropical\Model\Yuca\yuca-001"), new Vector3(0, 0, 220), Matrix.Identity));
			objects.Add(new PhysicalBody(this.Game.Content.Load<Microsoft.Xna.Framework.Graphics.Model>(@"Model\untitled_1"), new Vector3(35, 0, 35), Matrix.Identity));

			base.LoadContent( );
		}

		public void AddObject(Shared.Physics.IPhysicalBody gameObject) {
			if (!this.objects.Contains(gameObject))
				this.objects.Add(gameObject);
		}

		private void Hit(PhysicsObject item, Vector3 direction, float force) {
			item.Hit(this, direction, force);
		}

		public void Turn(PhysicalBody physicalBody, Rotation rotation, float value) {
			physicalBody.Turn(this, rotation, value);
		}

		public void Move(PhysicalBody physicalBody, Direction direction, float value) {
			physicalBody.Move(this, direction, value);
		}
	}
}
